package com.xnyzc.lhy.mis.service.impl.cms.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.esotericsoftware.minlog.Log;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.config.CashWithdrawalConfigParam;
import com.xnyzc.lhy.mis.entity.cms.config.OaSysCashWithdrawalConfig;
import com.xnyzc.lhy.mis.mapper.config.OaSysCashWithdrawalConfigMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.mis.service.cms.config.IOaSysCashWithdrawalConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 提现规则 服务实现类
 * </p>
 *
 * @author baoyaze
 * @since 2019-10-20
 */
@Slf4j
@Service
public class OaSysCashWithdrawalConfigServiceImpl extends ServiceImpl<OaSysCashWithdrawalConfigMapper, OaSysCashWithdrawalConfig> implements IOaSysCashWithdrawalConfigService
{
    @Autowired
    private OaSysCashWithdrawalConfigMapper oaSysCashWithdrawalConfigMapper;

    @Override
    public Rv getCashWithdrawalConfigList(CashWithdrawalConfigParam configParam)
    {
//        QueryWrapper<OaSysCashWithdrawalConfig> wrapper = new QueryWrapper<OaSysCashWithdrawalConfig>();
//        if(StringUtils.isNotEmpty(configParam.getCityId()))
//        {
//            wrapper.eq("city_id", configParam.getCityId());
//        }
//        IPage result = baseMapper.selectPage(configParam.getPage(), wrapper);

        IPage result = configParam.getPage();
        Map<String, Object> params = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(configParam.getCityId()))
        {
            params.put(CashWithdrawalConfigParam.CITY_ID, configParam.getCityId());
        }
        result.setRecords(baseMapper.queryList(result, params));
        return Rv.wrap(EErrorCode.success, result);
    }

    /**
     * 根据司机ID获取提现规则
     * @param driverId 司机ID
     * @return Rv
     */
    @Override
    public OaSysCashWithdrawalConfig getCashWithdrawalConfig(Long driverId) {

        OaSysCashWithdrawalConfig oaSysCashWithdrawalConfig = null;
        try {
            oaSysCashWithdrawalConfig = oaSysCashWithdrawalConfigMapper.queryCashWithdrawalConfig(driverId);
        } catch (Exception e) {
            log.error("请先配置提现规则: {}",e);
            throw PangException.create(EErrorCode.notConfigured);
        }
        if (CheckUtil.objIsEmpty(oaSysCashWithdrawalConfig)) {
            throw PangException.create(EErrorCode.notConfigured);
        }
        return oaSysCashWithdrawalConfig;
    }

    @Override
    public Rv getCashWithdrawalConfigInfo(Long configId)
    {
        return Rv.wrap(EErrorCode.success, baseMapper.selectById(configId));
    }

    @Override
    @Transactional
    public Rv addConfig(OaSysCashWithdrawalConfig cashWithdrawalConfig)
    {
        if(StringUtils.isNotEmpty(cashWithdrawalConfig.getCityId()))
        {
            QueryWrapper<OaSysCashWithdrawalConfig> wrapper = new QueryWrapper<OaSysCashWithdrawalConfig>();
            wrapper.eq("city_id", cashWithdrawalConfig.getCityId());
            OaSysCashWithdrawalConfig temp = baseMapper.selectOne(wrapper);
            if(temp == null)
            {
                // ?可以设置通用方法，赋值 tag、createUser、createTime、updateTime?
                String tag = TokenUtil.getTag();
                if (CheckUtil.objIsEmpty(tag)) {
                    throw PangException.create(EErrorCode.missingAuthArgs);
                }
                cashWithdrawalConfig.setTag(tag);
                cashWithdrawalConfig.setCreateUser(TokenUtil.getTokenUserId());
                cashWithdrawalConfig.setCreateTime(new Date());
                cashWithdrawalConfig.setUpdateTime(new Date());
                cashWithdrawalConfig.setVersion(0L);
                cashWithdrawalConfig.setDeleted(0);
                cashWithdrawalConfig.setWithdrawalConfigId(IDUtil.nextId());
                baseMapper.insert(cashWithdrawalConfig);
            }
            else
            {
                return Rv.error(EErrorCode.existCashWithdrawalConfigInCity);
            }
        }
        else
        {
            return Rv.error(EErrorCode.notCityIncashWithdrawalConfig);
        }

        return Rv.wrap(EErrorCode.success);
    }

    @Override
    @Transactional
    public Rv updateConfig(OaSysCashWithdrawalConfig cashWithdrawalConfig)
    {
        baseMapper.updateById(cashWithdrawalConfig);
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    @Transactional
    public Rv deleteConfig(OaSysCashWithdrawalConfig cashWithdrawalConfig)
    {
        baseMapper.deleteById(cashWithdrawalConfig.getWithdrawalConfigId());
        return Rv.wrap(EErrorCode.success);
    }
}
